package com.dwd.org.controller;


import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.PageBean;
import com.dwd.org.pojo.DictionaryDetail;
import com.dwd.org.query.DictionaryDetailQuery;
import com.dwd.org.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/15 15:04
 * @Version 1.0
 */

@RestController
@RequestMapping("/dictDetail")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 新增或者修改
     *
     * @param dictionaryDetail 如果id为空，则是新增，如果id不为空，则是修改
     * @return 返回成功还是失败
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody DictionaryDetail dictionaryDetail) {

        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.save(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
        return AjaxResult.me();
    }


    /**
     * 批量删除
     *
     * @param dictionaryDetails
     * @return
     */
    @PostMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody List<DictionaryDetail> dictionaryDetails) {
        try {
            //批量删除
            dictionaryDetailService.batchRemove(dictionaryDetails);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionaryDetailService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
        return AjaxResult.me();
    }

    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id) {
        return dictionaryDetailService.queryById(id);
    }

    @GetMapping
    public List<DictionaryDetail> queryAll() {
        return dictionaryDetailService.queryAll();
    }

    @PostMapping("/{id}")
    public PageBean<DictionaryDetail> queryPage(@PathVariable("id") Long id, @RequestBody DictionaryDetailQuery query) {
        return dictionaryDetailService.queryPage(query, id);
    }

}
